
import java.util.ArrayList;

public class Bank {

    public static void main(String[] args) {
        int time = 0;
        ArrayList<Client> sortedClients = new ArrayList<Client>();

        //Generates the 50 clients for the bank simulation
        for (int i = 0; i < 50; i++) {
            Client newClient = new Client((int) Math.round(Math.random() * 480), (int) Math.round(Math.random() * 30));
            sortedClients.add(newClient);
        }

        Client[] SortedClientsArray = new Client[sortedClients.size()];
        sortedClients.toArray(SortedClientsArray);

        //Sorts the clients
        for (int each = 0; each < SortedClientsArray.length; each++) {
            for (int other = each + 1; other < SortedClientsArray.length; other++) {
                Client anotherPerson = SortedClientsArray[other];
                if (SortedClientsArray[each].compareTo(anotherPerson) < 0) { //then, switch
                    Client currentPerson = SortedClientsArray[each];
                    SortedClientsArray[each] = SortedClientsArray[other];
                    SortedClientsArray[other] = currentPerson;
                }
            }
        }

        //>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
        //Cambiar aqui (Opcional)
        //>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
        //QueueArrayImplementation<Client> list = new QueueArrayImplementation()<Client>;
        CircularListQueueImplementation<Client> list = new CircularListQueueImplementation<Client>();

        for (int i = 0; i < 50; i++) {
            //System.out.println(SortedClientsArray[i].t1 + ", " + SortedClientsArray[i].t2);
            list.add(SortedClientsArray[i]);
        }


        /*******************************************************
         * ///////////////////////////////////////////////////
         * AUXILIAR: CAMBIAR AQUI (DESCOMENTAR)
         * ///////////////////////////////////////////////////
         ******************************************************/
        //QueueArrayImplementation<Client> cola1 = new QueueArrayImplementation()<Client>;
        //QueueArrayImplementation<Client> cola2 = new QueueArrayImplementation()<Client>;
        //QueueArrayImplementation<Client> cola3 = new QueueArrayImplementation()<Client>;
        //QueueArrayImplementation<Client> cola4 = new QueueArrayImplementation()<Client>;
        CircularListQueueImplementation<Client> cola1 = new CircularListQueueImplementation<Client>();
        CircularListQueueImplementation<Client> cola2 = new CircularListQueueImplementation<Client>();
        CircularListQueueImplementation<Client> cola3 = new CircularListQueueImplementation<Client>();
        CircularListQueueImplementation<Client> cola4 = new CircularListQueueImplementation<Client>();

        int count = 1;

        //Main simulation loop
        while (time < 480) {
            try {
                if (list.peek().t1 == time) {
                    switch (count) {
                        case 1: {
                            cola1.add(list.remove());
                            System.out.println("Ingreso un cliente en la cola 1");
                        }
                        case 2: {
                            cola2.add(list.remove());
                            System.out.println("Ingreso un cliente en la cola 2");
                        }
                        case 3: {
                            cola3.add(list.remove());
                            System.out.println("Ingreso un cliente en la cola 3");
                        }
                        case 4: {
                            cola4.add(list.remove());
                            System.out.println("Ingreso un cliente en la cola 4");
                        }
                    }
                    if (count == 4) {
                        count = 1;
                    } else {
                        count++;
                    }

                }
            } catch (Exception e) {
            }

            try {
                if ((time - cola1.peek().t1) == cola1.peek().t2) {
                    cola1.remove();
                    System.out.println("Se retiro un cliente de la cola 1");
                }
                if ((time - cola2.peek().t1) == cola2.peek().t2) {
                    cola2.remove();
                    System.out.println("Se retiro un cliente de la cola 2");
                }
                if ((time - cola3.peek().t1) == cola3.peek().t2) {
                    cola3.remove();
                    System.out.println("Se retiro un cliente de la cola 3");
                }
                if ((time - cola4.peek().t1) == cola4.peek().t2) {
                    cola4.remove();
                    System.out.println("Se retiro un cliente de la cola 4");
                }

            } catch (Exception E) {
            }




            time++;
        }

        int sum=0;

        for(int k=0 ; k<50 ; k++){
            sum += SortedClientsArray[k].t2;
        }
        
        int promedio =  sum/50;

        System.out.println("\n>>>>>>>>>>>>>>>>>>>>>>>>ESTADISTICAS<<<<<<<<<<<<<<<<<<<<<<<<<<<<<");
        System.out.println("El tiempo total de todos los clientes fue: " + sum);
        System.out.println("El tiempo promedio por cada cliente fue: " + promedio);



    }
}

class Client implements Comparable<Client> {

    int t1;
    int t2;

    public Client(int t1, int t2) {
        this.t1 = t1;
        this.t2 = t2;
    }

    public int compareTo(Client o) {
        if (o.t1 < this.t1) {
            return -1;
        } else if (o.t1 > this.t1) {
            return 1;
        } else {
            return 0;
        }
    }

    @Override
    public String toString() {
        return "(" + t1 + ", " + t2 + ")";
    }
}
